
<div id="datatable-wrapper" style="margin: 2px; display: none">
	<table id="datatable" style="width: 100%;">
		<thead>
			<tr>
				{foreach item="header" from=$data.table_headers}
					<th>
						{$header}
					</th>
				{/foreach}
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

{notAdmin}
{if !isset($data.sub['sub'])}
	<div id="welcome-message" style="padding: 16px; display: block;">
		Vitajte v evidenčnom systéme EIPV. <br/>
		Pokračujte vyplnením hľadaného výrazu <br/>
		<hr />
		Máte {$smarty.session.warnings} upozornení <a href="home?list=warnings">Prejdite na upozornenia</a><br/>
		Práve sledujete {$data.watch_count} položiek. <a href="home?list=tracking">Prejdite na sledované položky</a> <br/>
	</div>
{/if}
{/notAdmin}
{if isset($data.sub['sub'])}
{if $data.sub['sub'] eq 'profile'}
	<div>
		
		<div class="formular" style="display: inline-block; margin: 10px">
			<form action="home?action=apply" method="post" >
				<h2>Žiadosť o miestnosť</h2>
				<input type="submit" class="button" value="Pridať" />	
				<div class="form">
					<select name="room">
						{foreach from=$data.miestnosti item=foo}
						<option value='{$foo->room_id}'>{$foo->room_name}</option>
						{/foreach}
					</select> <br />
					sledovanie:<input type="checkbox" name="watch"/><br />
					pridávanie:<input type="checkbox" name="add"/><br />
					odstraňovanie:<input type="checkbox" name="delete"/><br />
					upravovanie:<input type="checkbox" name="edit"/><br /><br />
					
				</div>
				
			</form>
		</div>
		
		
		<div style="display: inline-block; margin: 10px">
			<form id="reg" action="home?action=newPass" method="post">
				<h2>Zmena hesla</h2>			
				zadajte nové heslo:<input id="password1" type="password" name="password1" />	<br />
				zopakujte nové heslo:<input type="password" name="password2" /> <br />
				<input type="submit" value="Zmeň" class="button" />
			</form>
		</div>
		<hr/>
		<div style="margin: 10px">
			<h2>Súčasné práva</h2>
			<table id="table" cellpadding="0" cellspacing="0" border="0" class="display">
			  <thead>
	          <tr>
	            <th>Miestnosť</th>
	            <th>Práva</th>
	          </tr>
	        </thead>
	        <tbody>
			{foreach from=$data.table item=foo}
			<tr>
			    <td>{$foo.room_name}</td>
			    <td>
			    	{if $foo.watch eq '1'}sledovanie | {/if}
			    	{if $foo.add eq '1'}pridávanie | {/if}
			    	{if $foo.edit eq '1'}upravovanie | {/if}
			    	{if $foo.delete eq '1'}mazanie {/if}
			    </td>
	    	</tr>
	    	{/foreach}
	    	</tbody>
			</table>
		</div>	
	</div>
{/if}
{/if}



<script type="text/javascript">
	var db_datatable;
	
	$(document).ready(function() {
    	$('#table').dataTable();
	} );
	
	   $(document).ready(function(){
   
   $("#reg").validate({
       rules: {
           password1: {
               required: true,
               minlength: 5
           },
           password2: {
               equalTo: "#password1"
           }
      },
      messages: {
      	password1:{ 
      	    required: "Musíte vložiť heslo.",
      		minlength: "Musí mať aspoň 5 znakov."
      	},
      	password2:{ 
      		equalTo: "Nezhoduje sa."
      	}
      }
   });
   })
	
	
	$(function(){
		{if isset($smarty.request.list) && $smarty.request.list=="tracking"}
			loadData(undefined, "getWatcheList");
		{/if}
		{if isset($smarty.request.list) && $smarty.request.list=="warnings"}
			loadData(undefined, "getWarningList");
		{/if}
	})
	
	function DeleteEquipment(obj){
		DeleteDialogOpen(obj.equipment_id);
	}
	
	function EditEquipment(obj){
		console.log(obj)
		EditDialogOpen(obj);
	}
	
	function loadData(query, action){
		$("#welcome-message").hide();
		$("#datatable-wrapper").show();
		if (typeof(action) == "undefined"){
			action = "processDataRequest";
		}
		db_datatable = $("#datatable").dataTable({
			"iDisplayLength": 50,
			"bDestroy": true,
			"bJQueryUI": true,
        	"sPaginationType": "full_numbers",
        	"bProcessing": true,
		    "sAjaxSource": AJAX_URL + "?controller=home/user&action=" + action + (typeof(query) !== "undefined" ? "&" + query : ""),
		    "sAjaxDataProp": "data",
		    //"sDom": '<"H"Tfr>t<"F"ip>',
		    /*"oTableTools": {
		    	"sSwfPath": "{$smarty.const.EXT_JS_PLUGIN_DIR}ui-datatables/swf/copy_cvs_xls_pdf.swf",
				"aButtons": [
					"copy", "csv", "xls", "pdf", "print"
				]
			},*/
			"oLanguage": {
		    	"sLengthMenu": "Zobraz _MENU_ výsledkov na stranu",
	            "sZeroRecords": "Žiaľ, nenašli sa žiadne výsledky vyhovujúce vašim požiadavkam.",
	            "sInfo": "Zobrazuje od _START_ do _END_ z _TOTAL_ výsledkov",
	            "sInfoEmpty": "Nezobrazujú sa žiadne výsledky.",
	            "sInfoFiltered": "(filtrované z _MAX_ výsledkov)",
	            "sSearch": "Použi filter:",
	            "oPaginate": {
	            	"sFirst" : "<<",
	            	"sPrevious" : "<",
	            	"sNext" : ">",
	            	"sLast" : ">>"
	            }
			},
			"fnInitComplete": function () {
				$("#admin-container").hide();
				{if $smarty.session.user.security_level >= $smarty.const.SECURITY_LEVEL_ADMIN}
					var isAdmin = true;
				{else}
					var isAdmin = false;
				{/if}	
				$( db_datatable.fnGetNodes() ).find(".db_table_tools").each(
					function(i){
						var serialized = $(this).html();
						$(this).html("").show();
						var unserialized = Object();
						var f = serialized.split("';'");
						for(var i in f){
							var item = f[i];
							var s = item.split("'='");
							
							unserialized[s[0]] = s[1]; 
						}
						
						edit_enable = (unserialized.edit == 1 || isAdmin) ? true : false;
						var edit_button = $("<button title='Editovať položku'>Edit</button>").button(
							{
								disabled: !edit_enable,
								text: false,
								icons:{
									primary: "ui-icon-pencil"
								}
								
							}
						).bind({
							click: function(){
								EditEquipment(unserialized);
							}	
						}).appendTo($(this));
						
						delete_enable = (unserialized.delete == 1 || isAdmin) ? true : false;
						var delete_button = $("<button title='Zmazať položku'>Delete</button>").button(
							{
								disabled: !delete_enable,
								text: false,
								icons:{
									primary: "ui-icon-closethick"
								}
							}
						).bind({
							click: function(){
								DeleteEquipment(unserialized);
							}	
						}).appendTo($(this));
						
						
					}
				);
	        },
		    "aoColumns": [
		   		{foreach item="header" key="key" from=$data.table_headers}
					{literal}{
						{/literal}
							{if $key != "tools"}
								"mDataProp" : "{$key}",
							{else}
								"bSortable": false,
							{/if}
							{if $key == 'date_added'}
								"fnRender": function ( oObj ) {
									var time = new Date(oObj.aData.date_added * 1000)
									var curr_date = time.getDate();
									var curr_month = time.getMonth() + 1; //months are zero based
									var curr_year = time.getFullYear();
									return curr_date + ". " + curr_month + ". " + curr_year;
								}
							{/if}
							{if $key == 'equipment_info'}
								"fnRender": function ( oObj ) {
									var c = oObj.aData.equipment_info;
									oObj.aData.info_full = c;
									if (c === null){
										return ""
									}
									if (c.length > 10){
										c = c.substring(0, 7) + "...";
									}
									return c;
								}
							{/if}
							{if $key == 'tools'}
								{literal}
									"fnRender": function ( oObj ) {
										var wrapper = $("<span class='db_table_tools'></span>").css({
											"display": "none",
											"width": "60px",
											"margin": "auto"
										});
										var serialized = "";
										for(var k in oObj.aData){
											serialized += k + "'='" + oObj.aData[k] + "';'"; 
										}
										wrapper.html(serialized);
										return wrapper[0].outerHTML;
									}
								{/literal}
							{/if}
						{literal}
					}{/literal},				
				{/foreach}
		    ]
		});
	}	
</script>